package com.edge.pbx.view.summing;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.edge.pbxdsp.R;


public class PbxInputChannelView extends View {

    private int mWidth, mHeight,  mCenterX;
    private Drawable mDrawable;
    private String mName;
    private Paint mTextPaint;
    private int mTextTop;
    @SuppressLint("NewApi")
    public PbxInputChannelView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mTextPaint = new Paint();
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mTextPaint.setFakeBoldText(true);
        mTextPaint.setAntiAlias(true);
        mDrawable = context.getDrawable(R.drawable.pbx_summing_input_ch_bg);
    }


    public void setName(String name) {
        mName = name;
        postInvalidate();
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {

        return super.onCreateDrawableState(extraSpace );
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = right -left;
        mHeight = bottom -top;
        mCenterX = mWidth/2;

        int draww =  mDrawable.getMinimumWidth();
        int drawh =  mDrawable.getMinimumHeight();
        float xscale = (float) mWidth/draww;
        float yscale =  (float)mHeight/drawh;

        float scale = Math.min(xscale,yscale);
        int w = (int) (draww*scale);
        int h = (int) (drawh *scale);
        int l = (mWidth - w)/2;
        int t = (mHeight - h)/2;
        mDrawable.setBounds(l,t, l+w, t+h);


        mTextPaint.setTextSize(h*0.26f);
//        mTextPaint.setStrokeWidth(h*0.01f);

        Paint.FontMetrics metrics = mTextPaint.getFontMetrics();
//        int height1 = (int) (mNumWidth/2 );

        mTextTop = (int) ((mHeight-h)/2 +(int) (h*0.81f) - metrics.descent +(metrics.bottom-metrics.top)/2);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled())
            return false;
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                setAlpha(0.4f);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                setAlpha(1.0f);
                break;
        }
        return super.onTouchEvent(event);
    }
// 228 41  0.82
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (isSelected()){
            mDrawable.setState(SELECTED_STATE_SET);
            mTextPaint.setColor( PbxOutputSelectChannelsLayout.sColorSelected );
        }else {
            mDrawable.setState(EMPTY_STATE_SET);
            mTextPaint.setColor( Color.WHITE);
        }
        if (mName != null){

            canvas.drawText(mName, mCenterX,mTextTop, mTextPaint);
        }
        mDrawable.draw(canvas);
    }

}
